package com.webex.ta.hydra.gui.action;

import com.webex.ta.hydra.core.TestPlan;
import com.webex.ta.hydra.core.TestPlanValidator;
import com.webex.ta.hydra.core.engine.DefaultHydraEngine;
import com.webex.ta.hydra.core.engine.ResultCollector;
import com.webex.ta.hydra.exceptions.IllegalKeywordException;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.debug.DebugContext;
import com.webex.ta.hydra.gui.project.HydraProjectSuite;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-27
 * Time: 10:02:47
 */
public class RunAction implements ActionHandler {
    private static Set<String> commands = new HashSet<String>();
	static {
		commands.add(ActionNames.DEBUG);
        commands.add(ActionNames.RUN);
        commands.add(ActionNames.PAUSE);
        commands.add(ActionNames.RESUME);
        commands.add(ActionNames.STOP);
	}

    private DefaultHydraEngine engine;

    public void doAction(ActionEvent e) {
        GuiPackage gp = GuiPackage.getInstance();
        if (ActionNames.DEBUG.equals(e.getActionCommand()) ||
                ActionNames.RUN.equals(e.getActionCommand())) {
            try {
                HydraProjectSuite project = gp.getActiveProject();
                if (project == null) return;
                TestPlan tp = project.getModel().makeTestPlan();
                if (!TestPlanValidator.validateTestPlan(tp)) {
                    int response = JOptionPane.showConfirmDialog(GuiPackage.getInstance().getIdeFrame(),
							"There are one or more errors in your script, continue to execute?",
							"Continue?",
							JOptionPane.YES_NO_OPTION,
							JOptionPane.QUESTION_MESSAGE);
                    if (response == JOptionPane.CLOSED_OPTION || response == JOptionPane.NO_OPTION)
                        return;
                }
                ActionRouter.getInstance().doActionNow(new ActionEvent(e.getSource(), e.getID(), ActionNames.CLEAR));
                engine = new DefaultHydraEngine(tp);
                if (ActionNames.DEBUG.equals(e.getActionCommand()))
                    engine.turnOnDebug();
                else
                    engine.turnOffDebug();
                engine.addTestListener(gp.getIdeFrame());
                ResultCollector.getInstance().clear();
                engine.addActionListener(ResultCollector.getInstance());
                engine.addActionListener(gp.getIdeFrame().getResultPane());
                engine.addAssertionListener(gp.getIdeFrame().getResultPane());
                DebugContext.getInstance().setDebugProject(project);
                engine.run();
            } catch (IllegalKeywordException e1) {
                e1.printStackTrace();
            }
        } else if (ActionNames.RESUME.equals(e.getActionCommand())) {
            if (engine != null)
                engine.resume();
        } else if (ActionNames.STOP.equals(e.getActionCommand())) {
            if (engine != null)
                engine.stop();
        }
    }

    public Set<String> getActionNames() {
        return commands;
    }
}
